/*
 * GClubAbstractCupHistoryRecord.java
 * Created on 15 nov. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.club;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;

import com.hockeo.vhbase.data.dao.contest.GContestRound;
import com.hockeo.vhbase.data.dao.season.GSeason;
import com.hockeo.vhbase.data.helper.GContestRoundFormat;

/**
 * Base class for all club history implementations for cup competitions (national and
 * international).
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
public abstract class GClubAbstractCupHistoryRecord extends GClubHistoryRecord
{
  @Column( name = "ac_assigned_round_id" )
  private String              d_strAssignedRoundId;

  @Column( name = "ac_seeding" )
  private int                 d_nSeeding;

  /**
   * Format of the last round that has been added.
   */
  @Enumerated( EnumType.STRING )
  @Column( name = "ac_last_round_format" )
  private GContestRoundFormat d_lastRoundFormat;

  /**
   * Name of the last played KO match (optional).
   */
  @Column( name = "ac_last_ko_match_name" )
  private String              d_strLastKOMatchName;

  /**
   * Indicates the label of the pool if the previous round was a round robin round.
   */
  @Column( name = "ac_last_pool" )
  private String              d_strLastPool;

  /**
   * Indicates the rank within the pool of the last round if the previous round was a
   * round robin round.
   */
  @Column( name = "ac_last_pool_rank" )
  private int                 d_nLastPoolRank;

  @Column( name = "ac_quali_rounds" )
  private int                 d_nNumQualificationRounds = 0;

  /** Default parameterless constructor. */
  protected GClubAbstractCupHistoryRecord()
  {}

  /**
   * Constructs a new GClubAbstractCupHistoryRecord.
   *
   * @param season the season for which to create the record
   * @param club the club to attache to the created record
   * @param round the (cup) round to which the club is initially assigned
   */
  public GClubAbstractCupHistoryRecord( GSeason season, GClub club, GContestRound round )
  {
    super( season, club, round.getContest() );

    assignToRound( round );
  }

  /**
   * Marks the club that holds this history object to be assigned to the specified cup
   * round.
   *
   * @param round the national cup round to which the club should be assigned
   */
  public void assignToRound( GContestRound round )
  {
    d_strAssignedRoundId = round.getId();
    setReachedRound( round.getRoundType() );
  }

  /**
   * Returns the ID of the cup contest round the club being the holder of this history
   * object is attached to.
   */
  public String getAssignedRoundId()
  {
    return d_strAssignedRoundId;
  }

  public int getSeeding()
  {
    return d_nSeeding;
  }

  /**
   * Sets the seeding for the club being the holder of this contest/history element.
   *
   * @param nSeeding the seeding value
   */
  public void setSeeding( int nSeeding )
  {
    d_nSeeding = nSeeding;
  }

  public GContestRoundFormat getLastRoundFormat()
  {
    return d_lastRoundFormat;
  }

  protected void setLastRoundFormat( GContestRoundFormat lastRoundFormat )
  {
    d_lastRoundFormat = lastRoundFormat;
  }

  public String getLastKOMatchName()
  {
    return d_strLastKOMatchName;
  }

  protected void setLastKOMatchName( String lastKOMatchName )
  {
    d_strLastKOMatchName = lastKOMatchName;
  }

  public String getLastPool()
  {
    return d_strLastPool;
  }

  protected void setLastPool( String strLastPoolLabel )
  {
    d_strLastPool = strLastPoolLabel;
  }

  public int getLastPoolRank()
  {
    return d_nLastPoolRank;
  }

  protected void setLastPoolRank( int lastPoolRank )
  {
    d_nLastPoolRank = lastPoolRank;
  }

  /**
   * Indicates whether this club played in the qualification round or not.
   */
  public boolean hasPlayedQuali()
  {
    return getNumQualificationRounds() > 0;
  }

  /**
   * Returns the number of played qualification rounds.
   */
  public int getNumQualificationRounds()
  {
    return d_nNumQualificationRounds;
  }

  /**
   * Sets the number of played qualification rounds.
   *
   * @param nRounds the number of qualification rounds
   */
  protected void setNumQualificationRounds( int nRounds )
  {
    d_nNumQualificationRounds = nRounds;
  }

  /**
   * Calls the super class' method {@link #reset()} and resets specific fields of this
   * class in addition.
   */
  @Override
  public void reset()
  {
    super.reset();

    d_nNumQualificationRounds = 0;
  }

  @Override
  public int compareTo( GClubHistoryRecord other )
  {
    int nTmp = super.compareTo( other );
    if ( nTmp != 0 )
      return nTmp;

    // Special cup/intcup criteria: higher number of qualification rounds (DESC)
    //
    return ( (GClubAbstractCupHistoryRecord) other ).getNumQualificationRounds() - this.getNumQualificationRounds();
  }
}
